package com.hehan.hhdemo.service.impl;

import com.hehan.hhdemo.common.resp.ModelResp;
import com.hehan.hhdemo.mq.producer.JMSProducer;
import com.hehan.hhdemo.po.EsConfigEntity;
import com.hehan.hhdemo.service.IESService;
import com.hehan.hhdemo.service.IJestElasticsearchManageRepository;
import io.searchbox.client.JestClient;
import io.searchbox.core.Search;
import io.searchbox.core.SearchResult;
import org.apache.activemq.command.ActiveMQQueue;
import org.elasticsearch.index.query.QueryStringQueryBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.jms.Destination;
import java.io.IOException;
import java.util.List;

import static org.elasticsearch.search.aggregations.metrics.percentiles.Percentiles.TYPE_NAME;

@Service
public class ESServiceImpl implements IESService {
    private static final Logger LOGGER = LoggerFactory.getLogger(ESServiceImpl.class);

    @Autowired
    private JestClient jestClient;

    @Autowired
    private IJestElasticsearchManageRepository jestElasticsearchManageRepository;

    @Autowired
    private RedisTemplate<String, String> redisTemplate;


    @Override
    public List<ModelResp> queryList() {
        QueryStringQueryBuilder queryStringQueryBuilder = new QueryStringQueryBuilder(QueryStringQueryBuilder.NAME);

        Search execute = new Search.Builder(queryStringQueryBuilder.queryString())
                .addIndex("model")
                .addType(TYPE_NAME)
                .build();

        SearchResult searchResult = null;

        try {
            searchResult = jestClient.execute(execute);
        } catch (IOException e) {
            LOGGER.error("search error", e);
        }

        LOGGER.info("search result:" + (null != searchResult ? searchResult.getJsonString() : "null"));

        return null;
    }

    @Override
    public List<ModelResp> testRedis() {

        redisTemplate.opsForValue().set("a", "1");
        Object a = redisTemplate.opsForValue().get("a");
        System.out.println(a);

        return null;
    }

    @Override
    public void delEsById(Integer id) {
        EsConfigEntity es =new EsConfigEntity();
        es.setHandleStr(id.toString());
        es.setEsIndex("model");
        es.setEsType("modeldata");
        jestElasticsearchManageRepository.deleteEntityByIdToEs(es);

    }


}
